<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>50-jQuery的事件机制和其他知识</title>
  
    
      <meta 
        property="og:title" 
        content="50-jQuery的事件机制和其他知识">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/50-jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E6%9C%BA%E5%88%B6%E5%92%8C%E5%85%B6%E4%BB%96%E7%9F%A5%E8%AF%86/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E8%AE%BE%E7%BD%AE%E5%AE%BD%E5%BA%A6%E5%92%8C%E9%AB%98%E5%BA%A6"><span class="toc-text">jQuery 设置宽度和高度</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E5%9D%90%E6%A0%87%E6%93%8D%E4%BD%9C"><span class="toc-text">jQuery 的坐标操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#offset-%E6%96%B9%E6%B3%95"><span class="toc-text">offset()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#position-%E6%96%B9%E6%B3%95"><span class="toc-text">position()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollTop-%E6%96%B9%E6%B3%95"><span class="toc-text">scrollTop()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollLeft-%E6%96%B9%E6%B3%95"><span class="toc-text">scrollLeft()方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E6%9C%BA%E5%88%B6"><span class="toc-text">jQuery的事件机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-text">常见的事件绑定</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#on%E6%96%B9%E5%BC%8F%E7%BB%91%E5%AE%9A%E4%BA%8B%E4%BB%B6"><span class="toc-text">on方式绑定事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#off%E6%96%B9%E5%BC%8F%E8%A7%A3%E7%BB%91%E4%BA%8B%E4%BB%B6"><span class="toc-text">off方式解绑事件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%AF%B9%E8%B1%A1"><span class="toc-text">jQuery的事件对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E4%B8%A4%E5%A4%A7%E7%89%B9%E7%82%B9"><span class="toc-text">jQuery 的两大特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#each%E7%9A%84%E7%94%A8%E6%B3%95"><span class="toc-text">each的用法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E5%BA%93%E5%85%B1%E5%AD%98"><span class="toc-text">多库共存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E6%8F%92%E4%BB%B6%E6%9C%BA%E5%88%B6"><span class="toc-text">jQuery 的插件机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E4%B9%8B%E6%94%B9%E5%8F%98%E9%A2%9C%E8%89%B2"><span class="toc-text">插件之改变颜色</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E4%B9%8B%E6%87%92%E5%8A%A0%E8%BD%BD"><span class="toc-text">插件之懒加载</span></a></li></ol></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      50-jQuery的事件机制和其他知识
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>4.1k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="jQuery-设置宽度和高度"><a href="#jQuery-设置宽度和高度" class="headerlink" title="jQuery 设置宽度和高度"></a>jQuery 设置宽度和高度</h2><p>宽度操作：</p>
<pre class="highlight"><span class="line">$(selector).<span class="title function_">height</span>();     <span class="comment">//不带参数表示获取高度</span></span><br><span class="line">$(selector).<span class="title function_">height</span>(<span class="number">200</span>);  <span class="comment">//带参数表示设置高度</span></span><br></pre>


<p>宽度操作：</p>
<pre class="highlight"><span class="line">$(selector).<span class="title function_">width</span>();     <span class="comment">//不带参数表示获取宽度</span></span><br><span class="line">$(selector).<span class="title function_">width</span>(<span class="number">200</span>);  <span class="comment">//带参数表示设置高宽度</span></span><br></pre>

<p><strong>问题</strong>：jQuery的css()获取高度，和jQuery的height获取高度，二者的区别？</p>
<p>答案：</p>
<pre class="highlight"><span class="line">$(<span class="string">&quot;div&quot;</span>).<span class="title function_">css</span>();     <span class="comment">//返回的是string类型，例如：30px</span></span><br><span class="line"></span><br><span class="line">$(<span class="string">&quot;div&quot;</span>).<span class="title function_">height</span>();  <span class="comment">//返回得失number类型，例如：30。常用于数学计算。</span></span><br></pre>

<p>如上方代码所示，<code>$(&quot;div&quot;).height();</code>返回的是number类型，常用于数学计算。</p>
<h2 id="jQuery-的坐标操作"><a href="#jQuery-的坐标操作" class="headerlink" title="jQuery 的坐标操作"></a>jQuery 的坐标操作</h2><h3 id="offset-方法"><a href="#offset-方法" class="headerlink" title="offset()方法"></a>offset()方法</h3><pre class="highlight"><span class="line">$(selector).<span class="title function_">offset</span>();</span><br><span class="line">$(selector).<span class="title function_">offset</span>(&#123;<span class="attr">left</span>:<span class="number">100</span>, <span class="attr">top</span>: <span class="number">150</span>&#125;);</span><br></pre>

<p>作用：获取或设置元素相对于 document 文档的位置。参数解释：</p>
<ul>
<li><p>无参数：表示获取。返回值为：{left:num, top:num}。返回值是相对于document的位置。</p>
</li>
<li><p>有参数：表示设置。参数建议使用 number 数值类型。</p>
</li>
</ul>
<p>注意：设置offset后，如果元素没有定位(默认值：static)，则被修改为relative。</p>
<h3 id="position-方法"><a href="#position-方法" class="headerlink" title="position()方法"></a>position()方法</h3><pre class="highlight"><span class="line">$(selector).<span class="title function_">position</span>();</span><br></pre>

<p>作用：获取相对于其最近的<strong>带有定位</strong>的父元素的位置。返回值为对象：<code>&#123;left:num, top:num&#125;</code>。</p>
<p>注意：只能获取，不能设置。</p>
<h3 id="scrollTop-方法"><a href="#scrollTop-方法" class="headerlink" title="scrollTop()方法"></a>scrollTop()方法</h3><pre class="highlight"><span class="line"><span class="title function_">scrollTop</span>();</span><br><span class="line">$(selector).<span class="title function_">scrollTop</span>(<span class="number">100</span>);</span><br></pre>

<p>作用：获取或者设置元素被卷去的头部的距离。参数解释：</p>
<ul>
<li><p>无参数：表示获取偏移。</p>
</li>
<li><p>有参数：表示设置偏移，参数为数值类型。</p>
</li>
</ul>
<h3 id="scrollLeft-方法"><a href="#scrollLeft-方法" class="headerlink" title="scrollLeft()方法"></a>scrollLeft()方法</h3><pre class="highlight"><span class="line"><span class="title function_">scrollLeft</span>();</span><br><span class="line">$(selector).<span class="title function_">scrollLeft</span>(<span class="number">100</span>);</span><br></pre>

<p>作用：获取或者设置元素水平方向滚动的位置。参数解释：</p>
<ul>
<li><p>无参数：表示获取偏移。</p>
</li>
<li><p>有参数：表示设置偏移，参数为数值类型。</p>
</li>
</ul>
<p>代码示范：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">body</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">5000px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.box1</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: relative;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">overflow</span>: auto;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: pink;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.box2</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">top</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">left</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: yellow;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery-1.11.1.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        $(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//距离页面最顶端或者最左侧的距离和有没有定位没有关系</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;button&quot;</span>).<span class="title function_">eq</span>(<span class="number">0</span>).<span class="title function_">click</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="title function_">alert</span>($(<span class="string">&quot;.box2&quot;</span>).<span class="title function_">offset</span>().<span class="property">top</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//距离页面最顶端或者最左侧的距离和有没有定位没有关系</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;button&quot;</span>).<span class="title function_">eq</span>(<span class="number">1</span>).<span class="title function_">click</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                $(<span class="string">&quot;.box2&quot;</span>).<span class="title function_">offset</span>(&#123;<span class="string">&quot;left&quot;</span>: <span class="number">1000</span>, <span class="string">&quot;top&quot;</span>: <span class="number">1000</span>&#125;);</span></span><br><span class="line"><span class="language-javascript">            &#125;)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//距离父系盒子中带有定位的盒子的距离(获取的就是定位值，和margin/padding无关)</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;button&quot;</span>).<span class="title function_">eq</span>(<span class="number">2</span>).<span class="title function_">click</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="title function_">alert</span>($(<span class="string">&quot;.box2&quot;</span>).<span class="title function_">position</span>().<span class="property">top</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//距离父系盒子中带有定位的盒子的距离(获取的就是定位值，和margin/padding无关)</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;button&quot;</span>).<span class="title function_">eq</span>(<span class="number">3</span>).<span class="title function_">click</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                $(<span class="string">&quot;.box2&quot;</span>).<span class="title function_">position</span>().<span class="property">top</span> = <span class="string">&quot;100px&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">            &#125;)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//获取被选取的头部</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;button&quot;</span>).<span class="title function_">eq</span>(<span class="number">4</span>).<span class="title function_">click</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="title function_">alert</span>($(<span class="variable language_">window</span>).<span class="title function_">scrollTop</span>());</span></span><br><span class="line"><span class="language-javascript">            &#125;)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//获取被选取的头部</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;button&quot;</span>).<span class="title function_">eq</span>(<span class="number">5</span>).<span class="title function_">click</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                $(<span class="variable language_">window</span>).<span class="title function_">scrollTop</span>(<span class="number">100</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box2&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>offset().top获取<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>offset().top设置<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>position().top获取<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>position().top设置<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>scrollTop()<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>scrollTop()<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<h2 id="jQuery的事件机制"><a href="#jQuery的事件机制" class="headerlink" title="jQuery的事件机制"></a>jQuery的事件机制</h2><h3 id="常见的事件绑定"><a href="#常见的事件绑定" class="headerlink" title="常见的事件绑定"></a>常见的事件绑定</h3><ul>
<li><p>click(handler) 				单击事件。</p>
</li>
<li><p>blur(handler) 				失去焦点事件。</p>
</li>
<li><p>mouseenter(handler) 		鼠标进入事件。</p>
</li>
<li><p>mouseleave(handler)			鼠标离开事件。</p>
</li>
<li><p>dbclick(handler) 			双击事件。</p>
</li>
<li><p>change(handler)  改变事件，如：文本框值改变，下拉列表值改变等。</p>
</li>
<li><p>focus(handler) 				获得焦点事件。</p>
</li>
<li><p>keydown(handler) 			键盘按下事件。</p>
</li>
</ul>
<p>参考链接：<a target="_blank" rel="noopener" href="http://www.w3school.com.cn/jquery/jquery_ref_events.asp">http://www.w3school.com.cn/jquery/jquery_ref_events.asp</a></p>
<h3 id="on方式绑定事件"><a href="#on方式绑定事件" class="headerlink" title="on方式绑定事件"></a>on方式绑定事件</h3><p>最早采用的是 bind、delegate等方式绑定的。jQuery 1.7版本后，jQuery用on统一了所有的事件处理的方法，此方法兼容zepto(移动端类似于jQuery的一个库)。</p>
<p>格式举例：</p>
<pre class="highlight"><span class="line">$(<span class="variable language_">document</span>).<span class="title function_">on</span>(<span class="string">&quot;click mouseenter&quot;</span>, <span class="string">&quot;.box&quot;</span>, &#123;<span class="string">&quot;name&quot;</span>: <span class="number">111</span>&#125;, <span class="keyword">function</span> (<span class="params">event</span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">data</span>);      <span class="comment">//event.data获取的就是第三个参数这个json。</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">data</span>.<span class="property">name</span>); <span class="comment">//event.data.name获取的是name的值。</span></span><br><span class="line">&#125;);</span><br></pre>

<p>参数解释：</p>
<ul>
<li><p>第一个参数：events，绑定事件的名称可以是由空格分隔的多个事件（标准事件或者自定义事件）。上方代码绑定的是单击事件和鼠标进入事件。</p>
</li>
<li><p>第二个参数：selector, 执行事件的后代元素。</p>
</li>
<li><p>第三个参数：data，传递给事件处理函数的数据，事件触发的时候通过event.data来使用（也就是说，可以通过event拿到data）</p>
</li>
<li><p>第四个参数：handler，事件处理函数。</p>
</li>
</ul>
<p>简单点的写法：</p>
<pre class="highlight"><span class="line">$(<span class="variable language_">document</span>).<span class="title function_">on</span>(<span class="string">&quot;click&quot;</span>,<span class="string">&quot;.box&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">   <span class="title function_">alert</span>(<span class="number">1</span>);</span><br><span class="line">&#125;);</span><br></pre>

<h3 id="off方式解绑事件"><a href="#off方式解绑事件" class="headerlink" title="off方式解绑事件"></a>off方式解绑事件</h3><pre class="highlight"><span class="line">$(selector).<span class="title function_">off</span>();      <span class="comment">// 解绑匹配元素的所有事件</span></span><br><span class="line"></span><br><span class="line">$(selector).<span class="title function_">off</span>(<span class="string">&quot;click&quot;</span>);   <span class="comment">// 解绑匹配元素的所有click事件</span></span><br><span class="line"></span><br><span class="line">$(selector).<span class="title function_">off</span>( <span class="string">&quot;click&quot;</span>, <span class="string">&quot;**&quot;</span> );   <span class="comment">// 解绑所有代理的click事件，元素本身的事件不会被解绑</span></span><br></pre>

<h2 id="jQuery的事件对象"><a href="#jQuery的事件对象" class="headerlink" title="jQuery的事件对象"></a>jQuery的事件对象</h2><p>event.data                  传递给事件处理程序的额外数据</p>
<p>event.currentTarget             等同于this，当前DOM对象</p>
<p>event.pageX                     鼠标相对于文档左部边缘的位置</p>
<p>event.target                    触发事件源，不一定&#x3D;&#x3D;&#x3D;this</p>
<p>event.stopPropagation()；    阻止事件冒泡</p>
<p>event.preventDefault();     阻止默认行为</p>
<p>event.type                  事件类型：click，dbclick…</p>
<p>event.which                     鼠标的按键类型：左1 中2 右3</p>
<p>event.keyCode               键盘按键代码</p>
<p>代码演示：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery-1.11.1.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        $(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            $(<span class="variable language_">document</span>).<span class="title function_">on</span>(<span class="string">&quot;click&quot;</span>, &#123;&#125;, <span class="keyword">function</span> (<span class="params">event</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">data</span>);</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">currentTarget</span>);</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">target</span>);</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">pageX</span>);</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">type</span>);</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">which</span>);</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">keyCode</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;);</span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>上方代码中，我们通过event参数，获取了点击事件的各种event里的属性。</p>
<p>单击网页后，打印结果为：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180205_2338.png" class="lozad post-image"src="http://img.smyhvae.com/20180205_2338.png"></p>
<p><strong>举例</strong>：键盘上对的按键按下时，变色</p>
<p>这个时候就要用到event参数，因为要获取event.keyCode，才能知道按下的是键盘上的哪个按键。</p>
<p>代码实现：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.wrap</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">100px</span> auto <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.wrap</span> <span class="selector-tag">h1</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.wrap</span> <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background</span>: pink;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font-size</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">line-height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery-1.11.1.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        $(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//需求：在页面上，按键.是哪个颜色的首写字母div就改变为该颜色，然后span内容赋值。</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//步骤：</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//1.给document绑定keyup事件</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//2.获取值，根据此值，给div和span上色和内容</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> div = $(<span class="string">&quot;#bgChange&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> span = $(<span class="string">&quot;#keyCodeSpan&quot;</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//绑定事件</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="variable language_">document</span>).<span class="title function_">keyup</span>(<span class="keyword">function</span> (<span class="params">e</span>) &#123;  <span class="comment">//键盘弹出时，触发事件</span></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//调用方法</span></span></span><br><span class="line"><span class="language-javascript">                <span class="title function_">setColor</span>(e.<span class="property">keyCode</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">function</span> <span class="title function_">setColor</span>(<span class="params">keyCode</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//alert(e.keyCode);</span></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//2.获取值，根据此值，给div和span上色和内容</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">switch</span> (keyCode) &#123;</span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">case</span> <span class="number">80</span>:</span></span><br><span class="line"><span class="language-javascript">                        <span class="comment">//修改内容pink</span></span></span><br><span class="line"><span class="language-javascript">                        <span class="title function_">setEle</span>(<span class="string">&quot;pink&quot;</span>, keyCode);</span></span><br><span class="line"><span class="language-javascript">                        <span class="keyword">break</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">case</span> <span class="number">66</span>:</span></span><br><span class="line"><span class="language-javascript">                        <span class="comment">//修改内容blue</span></span></span><br><span class="line"><span class="language-javascript">                        <span class="title function_">setEle</span>(<span class="string">&quot;blue&quot;</span>, keyCode);</span></span><br><span class="line"><span class="language-javascript">                        <span class="keyword">break</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">case</span> <span class="number">79</span>:</span></span><br><span class="line"><span class="language-javascript">                        <span class="comment">//修改内容orange</span></span></span><br><span class="line"><span class="language-javascript">                        <span class="title function_">setEle</span>(<span class="string">&quot;orange&quot;</span>, keyCode);</span></span><br><span class="line"><span class="language-javascript">                        <span class="keyword">break</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">case</span> <span class="number">82</span>:</span></span><br><span class="line"><span class="language-javascript">                        <span class="comment">//修改内容red</span></span></span><br><span class="line"><span class="language-javascript">                        <span class="title function_">setEle</span>(<span class="string">&quot;red&quot;</span>, keyCode);</span></span><br><span class="line"><span class="language-javascript">                        <span class="keyword">break</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">case</span> <span class="number">89</span>:</span></span><br><span class="line"><span class="language-javascript">                        <span class="comment">//修改内容yellow</span></span></span><br><span class="line"><span class="language-javascript">                        <span class="title function_">setEle</span>(<span class="string">&quot;yellow&quot;</span>, keyCode);</span></span><br><span class="line"><span class="language-javascript">                        <span class="keyword">break</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">default</span> :</span></span><br><span class="line"><span class="language-javascript">                        <span class="title function_">alert</span>(<span class="string">&quot;系统没有设置该颜色！&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">function</span> <span class="title function_">setEle</span>(<span class="params">a, b</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                    div.<span class="title function_">css</span>(<span class="string">&quot;background-color&quot;</span>, a);</span></span><br><span class="line"><span class="language-javascript">                    span.<span class="title function_">text</span>(b);</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//1.给document绑定keyup事件</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//            $(document).keyup(function (e) &#123;</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                //alert(e.keyCode);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                //2.获取值，根据此值，给div和span上色和内容</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                switch (e.keyCode)&#123;</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                    case 80:</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        //修改内容pink</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        div.css(&quot;background-color&quot;,&quot;pink&quot;);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        span.text(e.keyCode);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        break;</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                    case 66:</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        //修改内容blue</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        div.css(&quot;background-color&quot;,&quot;blue&quot;);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        span.text(e.keyCode);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        break;</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                    case 79:</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        //修改内容orange</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        div.css(&quot;background-color&quot;,&quot;orange&quot;);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        span.text(e.keyCode);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        break;</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                    case 82:</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        //修改内容red</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        div.css(&quot;background-color&quot;,&quot;red&quot;);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        span.text(e.keyCode);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        break;</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                    case 89:</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        //修改内容yellow</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        div.css(&quot;background-color&quot;,&quot;yellow&quot;);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        span.text(e.keyCode);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        break;</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                    default :</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                        alert(&quot;系统没有设置该颜色！&quot;);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                &#125;</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//            &#125;);</span></span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;wrap&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>按键改变颜色<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;bgChange&quot;</span>&gt;</span></span><br><span class="line">        keyCode为：</span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">id</span>=<span class="string">&quot;keyCodeSpan&quot;</span>&gt;</span>80<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre>


<h2 id="jQuery-的两大特点"><a href="#jQuery-的两大特点" class="headerlink" title="jQuery 的两大特点"></a>jQuery 的两大特点</h2><p>（1）<strong>链式编程</strong>：比如<code>.show()</code>和<code>.html()</code>可以连写成<code>.show().html()</code>。</p>
<p>链式编程原理：return this。</p>
<p>通常情况下，只有设置操作才能把链式编程延续下去。因为获取操作的时候，会返回获取到的相应的值，无法返回 this。</p>
<pre class="highlight"><span class="line"><span class="title function_">end</span>(); <span class="comment">// 结束当前链最近的一次过滤操作，并且返回匹配元素之前的状态。</span></span><br></pre>


<p>（2）<strong>隐式迭代</strong>：隐式 对应的是 显式。隐式迭代的意思是：在方法的内部会为匹配到的所有元素进行循环遍历，执行相应的方法；而不用我们再进行循环，简化我们的操作，方便我们调用。</p>
<p>如果获取的是多元素的值，大部分情况下返回的是第一个元素的值。</p>
<p><strong>举例：</strong>五角星评分</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>五角星评分案例<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        * &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.comment</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font-size</span>: <span class="number">40px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">color</span>: <span class="number">#ff3100</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.comment</span> <span class="selector-tag">li</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">cursor</span>: pointer;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">ul</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">list-style</span>: none;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery-1.11.1.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        $(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> star_none = <span class="string">&#x27;☆&#x27;</span>; <span class="comment">// 空心五角星</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> star_sel = <span class="string">&#x27;★&#x27;</span>; <span class="comment">// 实心五角星</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//需求1：鼠标放悬停时，当前的li和之前所有的li内容全部变为实心五角星，移开变为空心。</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;.comment li&quot;</span>).<span class="title function_">on</span>(<span class="string">&quot;mouseenter&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//当前五角星，和之前的所有五角星，全部是实心的，其他的为空心</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//写法一：分两次写</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                $(this).text(star_sel).prevAll(&quot;li&quot;).text(star_sel);</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//                $(this).nextAll(&quot;li&quot;).text(star_none);</span></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//写法二：一次性写完，但要用到end()，否则会出问题。【重要】</span></span></span><br><span class="line"><span class="language-javascript">                $(<span class="variable language_">this</span>).<span class="title function_">text</span>(star_sel).<span class="title function_">prevAll</span>(<span class="string">&quot;li&quot;</span>).<span class="title function_">text</span>(star_sel).<span class="title function_">end</span>().<span class="title function_">nextAll</span>(<span class="string">&quot;li&quot;</span>).<span class="title function_">text</span>(star_none);</span></span><br><span class="line"><span class="language-javascript">            &#125;);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;.comment li&quot;</span>).<span class="title function_">on</span>(<span class="string">&quot;mouseleave&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//bug：如果没有点击过li，那么会出现无法清除的现象，处理办法就是先判断，看看是否有current类</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">if</span> ($(<span class="string">&quot;li.current&quot;</span>).<span class="property">length</span> === <span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                    $(<span class="string">&quot;.comment li&quot;</span>).<span class="title function_">text</span>(star_none);</span></span><br><span class="line"><span class="language-javascript">                &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="language-javascript">                    <span class="comment">//当鼠标移开的时候，谁有current类名，那么当前和之前所有的li前部是实心五角星，后面的所有li都是空心</span></span></span><br><span class="line"><span class="language-javascript">                    $(<span class="string">&quot;li.current&quot;</span>).<span class="title function_">text</span>(star_sel).<span class="title function_">prevAll</span>(<span class="string">&quot;li&quot;</span>).<span class="title function_">text</span>(star_sel).<span class="title function_">end</span>().<span class="title function_">nextAll</span>(<span class="string">&quot;li&quot;</span>).<span class="title function_">text</span>(star_none);</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript">            &#125;);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//需求2：鼠标点击那个li，当你前li和之前所有的li都变成实心五角星，其他变为空心。</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;.comment li&quot;</span>).<span class="title function_">on</span>(<span class="string">&quot;click&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//点击哪个li给他加一个类名。清空其他所有的li的类名</span></span></span><br><span class="line"><span class="language-javascript">                $(<span class="variable language_">this</span>).<span class="title function_">attr</span>(<span class="string">&quot;class&quot;</span>, <span class="string">&quot;current&quot;</span>).<span class="title function_">siblings</span>(<span class="string">&quot;li&quot;</span>).<span class="title function_">removeAttr</span>(<span class="string">&quot;class&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        &#125;);</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;comment&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>☆<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>☆<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>☆<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>☆<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>☆<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre>


<p>上方代码中，注意end的用法，很巧妙。</p>
<p>实现效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180206_1100.gif" class="lozad post-image"src="http://img.smyhvae.com/20180206_1100.gif"></p>
<h2 id="each的用法"><a href="#each的用法" class="headerlink" title="each的用法"></a>each的用法</h2><p>大部分情况下是不需要使用each方法的，因为jQuery的隐式迭代特性。</p>
<p>但是，如果要对每个元素做不同的处理，这时候就用到了each方法。</p>
<p>格式如下：</p>
<pre class="highlight"><span class="line">$(selector).<span class="title function_">each</span>(<span class="keyword">function</span>(<span class="params">index,element</span>)&#123;&#125;);</span><br></pre>

<p>参数解释：</p>
<ul>
<li><p>参数一：表示当前元素在所有匹配元素中的索引号</p>
</li>
<li><p>参数二：参数二表示当前元素（是js 中的DOM对象，而不是jQuery对象）</p>
</li>
</ul>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">li</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">list-style</span>: none;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font</span>: <span class="number">50px</span>/<span class="number">100px</span> <span class="string">&quot;simsun&quot;</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">color</span>: white;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: black;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery-1.11.1.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">jQuery</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//设置每个不一样的盒子透明度逐渐递增</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;ul li&quot;</span>).<span class="title function_">each</span>(<span class="keyword">function</span> (<span class="params">index, element</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                $(element).<span class="title function_">css</span>(<span class="string">&quot;opacity&quot;</span>, (index + <span class="number">1</span>) / <span class="number">10</span>);</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(index+<span class="string">&quot;---&quot;</span>+element.<span class="property">tagName</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            &#125;);</span></span><br><span class="line"><span class="language-javascript">        &#125;);</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa1&quot;</span>&gt;</span>1<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa2&quot;</span>&gt;</span>2<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa3&quot;</span>&gt;</span>3<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa4&quot;</span>&gt;</span>4<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa5&quot;</span>&gt;</span>5<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa6&quot;</span>&gt;</span>6<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa7&quot;</span>&gt;</span>7<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa8&quot;</span>&gt;</span>8<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa9&quot;</span>&gt;</span>9<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;aaa10&quot;</span>&gt;</span>10<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180206_1110.png" class="lozad post-image"src="http://img.smyhvae.com/20180206_1110.png"></p>
<h2 id="多库共存"><a href="#多库共存" class="headerlink" title="多库共存"></a>多库共存</h2><p><strong>多库共存</strong>指的是：jQuery占用了 <code>$</code> 和 <code>jQuery</code> 这两个变量。当在同一个页面中引用了 jQuery 库以及其他的库（或者其他版本的jQuery库），恰好其他的库中也用到了 <code>$</code> 或者<code>jQuery</code>变量.那么，要保证每个库都能正常使用，就产生了多库共存的问题。</p>
<p>温馨提示：我们可以通过以下方式获取 jQuery 库的版本号。</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>($.fn.<span class="property">jquery</span>);  <span class="comment">//打印 jQuery 库的版本号</span></span><br></pre>

<p><strong>办法一</strong>：让 jQuery 放弃对 <code>$</code> 的使用权：</p>
<pre class="highlight"><span class="line">$.<span class="title function_">noConflict</span>();</span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180206_1126.png" class="lozad post-image"src="http://img.smyhvae.com/20180206_1126.png"></p>
<p>上图中，代码中同时包含了两个版本的库。1.11.1版本放弃了对 <code>$</code> 的使用权，交给了1.8.2版本；但是1.11.1版本并没有放弃对 <code>jQuery</code>关键字的使用权。</p>
<p>办法二：同时放弃放弃两个符号的使用权，并定义一个新的使用权（如果有三个库时，可以这样用）</p>
<pre class="highlight"><span class="line">$.<span class="title function_">noConflict</span>(<span class="literal">true</span>);   <span class="comment">//返回值是新的关键字</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180206_1133.png" class="lozad post-image"src="http://img.smyhvae.com/20180206_1133.png"></p>
<h2 id="jQuery-的插件机制"><a href="#jQuery-的插件机制" class="headerlink" title="jQuery 的插件机制"></a>jQuery 的插件机制</h2><p>jQuery 库，虽然功能强大，但也不是面面俱到。jQuery 是通过插件的方式，来扩展它的功能：</p>
<ul>
<li><p>当你需要某个插件的时候，你可以“安装”到jQuery上面，然后使用。</p>
</li>
<li><p>当你不再需要这个插件，那你就可以从jQuery上“卸载”它。</p>
</li>
</ul>
<h3 id="插件之改变颜色"><a href="#插件之改变颜色" class="headerlink" title="插件之改变颜色"></a>插件之改变颜色</h3><p>jQuery的自定义动画方法animate()，在执行动画时，是不支持设置背景色这个属性的。这个时候可以借助<code>jQuery.color.js</code>这个插件。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: blue;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery-1.11.1.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery.color.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        $(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//点击按钮，改变盒子的宽度和背景色</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;button&quot;</span>).<span class="title function_">on</span>(<span class="string">&quot;click&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                $(<span class="string">&quot;div&quot;</span>).<span class="title function_">animate</span>(&#123;<span class="string">&quot;width&quot;</span>: <span class="number">200</span>, <span class="string">&quot;background-color&quot;</span>: <span class="string">&quot;red&quot;</span>&#125;, <span class="number">2000</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                    <span class="title function_">alert</span>(<span class="string">&quot;动画结束&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">                &#125;);</span></span><br><span class="line"><span class="language-javascript">            &#125;);</span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>变色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180206_1400.gif" class="lozad post-image"src="http://img.smyhvae.com/20180206_1400.gif"></p>
<p>上方代码中，因为加入了一行插件：（注意顺序是放在jQuery插件之后）</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery.color.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre>

<p>否则的话，在动画执行的过程中，是无法设置背景色的。</p>
<h3 id="插件之懒加载"><a href="#插件之懒加载" class="headerlink" title="插件之懒加载"></a>插件之懒加载</h3><p>懒加载：当打开一个网页时，只有当我看到某个部分，再加载那个部分；而不是一下子全部加载完毕。这样可以优化打开的速度。</p>
<p>比如说，我可以设置一张图片为懒加载，于是，这张图片会等我宠幸到它的时候，它再打开。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">3000px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: pink;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery-1.11.1.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--懒加载的使用。第一步：导包(必须在jquery库的下方）--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;jquery.lazyload.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        $(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//第二步骤：调用懒加载的方法实现功能。参数的不同，功能也不同。</span></span></span><br><span class="line"><span class="language-javascript">            $(<span class="string">&quot;img.lazy&quot;</span>).<span class="title function_">lazyload</span>();</span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--需要实现将图片设置为懒加载模式--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">img</span> <span class="attr">class</span>=<span class="string">&quot;lazy&quot;</span> <span class="attr">data-original</span>=<span class="string">&quot;images/01.jpg&quot;</span> <span class="attr">width</span>=<span class="string">&quot;640&quot;</span> <span class="attr">height</span>=<span class="string">&quot;480&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>


  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/50-jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E6%9C%BA%E5%88%B6%E5%92%8C%E5%85%B6%E4%BB%96%E7%9F%A5%E8%AF%86/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/50-jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E6%9C%BA%E5%88%B6%E5%92%8C%E5%85%B6%E4%BB%96%E7%9F%A5%E8%AF%86/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/49-jQuery%E6%93%8D%E4%BD%9CDOM/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">49-jQuery操作DOM </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/51-Zepto%E5%85%A5%E9%97%A8/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">51-Zepto入门 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E8%AE%BE%E7%BD%AE%E5%AE%BD%E5%BA%A6%E5%92%8C%E9%AB%98%E5%BA%A6"><span class="toc-text">jQuery 设置宽度和高度</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E5%9D%90%E6%A0%87%E6%93%8D%E4%BD%9C"><span class="toc-text">jQuery 的坐标操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#offset-%E6%96%B9%E6%B3%95"><span class="toc-text">offset()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#position-%E6%96%B9%E6%B3%95"><span class="toc-text">position()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollTop-%E6%96%B9%E6%B3%95"><span class="toc-text">scrollTop()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollLeft-%E6%96%B9%E6%B3%95"><span class="toc-text">scrollLeft()方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E6%9C%BA%E5%88%B6"><span class="toc-text">jQuery的事件机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-text">常见的事件绑定</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#on%E6%96%B9%E5%BC%8F%E7%BB%91%E5%AE%9A%E4%BA%8B%E4%BB%B6"><span class="toc-text">on方式绑定事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#off%E6%96%B9%E5%BC%8F%E8%A7%A3%E7%BB%91%E4%BA%8B%E4%BB%B6"><span class="toc-text">off方式解绑事件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%AF%B9%E8%B1%A1"><span class="toc-text">jQuery的事件对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E4%B8%A4%E5%A4%A7%E7%89%B9%E7%82%B9"><span class="toc-text">jQuery 的两大特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#each%E7%9A%84%E7%94%A8%E6%B3%95"><span class="toc-text">each的用法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E5%BA%93%E5%85%B1%E5%AD%98"><span class="toc-text">多库共存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E6%8F%92%E4%BB%B6%E6%9C%BA%E5%88%B6"><span class="toc-text">jQuery 的插件机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E4%B9%8B%E6%94%B9%E5%8F%98%E9%A2%9C%E8%89%B2"><span class="toc-text">插件之改变颜色</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E4%B9%8B%E6%87%92%E5%8A%A0%E8%BD%BD"><span class="toc-text">插件之懒加载</span></a></li></ol></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E8%AE%BE%E7%BD%AE%E5%AE%BD%E5%BA%A6%E5%92%8C%E9%AB%98%E5%BA%A6"><span class="toc-text">jQuery 设置宽度和高度</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E5%9D%90%E6%A0%87%E6%93%8D%E4%BD%9C"><span class="toc-text">jQuery 的坐标操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#offset-%E6%96%B9%E6%B3%95"><span class="toc-text">offset()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#position-%E6%96%B9%E6%B3%95"><span class="toc-text">position()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollTop-%E6%96%B9%E6%B3%95"><span class="toc-text">scrollTop()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollLeft-%E6%96%B9%E6%B3%95"><span class="toc-text">scrollLeft()方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E6%9C%BA%E5%88%B6"><span class="toc-text">jQuery的事件机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-text">常见的事件绑定</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#on%E6%96%B9%E5%BC%8F%E7%BB%91%E5%AE%9A%E4%BA%8B%E4%BB%B6"><span class="toc-text">on方式绑定事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#off%E6%96%B9%E5%BC%8F%E8%A7%A3%E7%BB%91%E4%BA%8B%E4%BB%B6"><span class="toc-text">off方式解绑事件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%AF%B9%E8%B1%A1"><span class="toc-text">jQuery的事件对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E4%B8%A4%E5%A4%A7%E7%89%B9%E7%82%B9"><span class="toc-text">jQuery 的两大特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#each%E7%9A%84%E7%94%A8%E6%B3%95"><span class="toc-text">each的用法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E5%BA%93%E5%85%B1%E5%AD%98"><span class="toc-text">多库共存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E7%9A%84%E6%8F%92%E4%BB%B6%E6%9C%BA%E5%88%B6"><span class="toc-text">jQuery 的插件机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E4%B9%8B%E6%94%B9%E5%8F%98%E9%A2%9C%E8%89%B2"><span class="toc-text">插件之改变颜色</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E4%B9%8B%E6%87%92%E5%8A%A0%E8%BD%BD"><span class="toc-text">插件之懒加载</span></a></li></ol></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
